//
//  Custom_Extension.swift
//  ShiLi_Book
//
//  Created by alyadan on 2017/3/30.
//  Copyright © 2017年 alyadan. All rights reserved.
//

import Foundation
import UIKit

import CoreImage
import CommonCrypto


public extension UINavigationController {
    open override var childForStatusBarHidden: UIViewController? {
        return viewControllers.last
    }
    
    open override var childForStatusBarStyle: UIViewController? {
        return viewControllers.last
    }
}



//placeHolder
var textField_PlaceHolder_Color:Int = 0

public extension UITextField {
    
    var placeHolderColor: UIColor? {
        get {
            return objc_getAssociatedObject(self, &textField_PlaceHolder_Color) as? UIColor
        }
        
        set {
            objc_setAssociatedObject(self, &textField_PlaceHolder_Color, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    func yd_setPlaceHolder(str: String, attributes: [NSAttributedString.Key: Any]?  = nil) {
        
        
        if let color = self.placeHolderColor {
            let attr = attributes ?? [
                NSAttributedString.Key.font: FontSizeType.normal.font(bold: true),
                NSAttributedString.Key.foregroundColor: color
            ]
            self.attributedPlaceholder = NSAttributedString(string: str, attributes: attr)
            
        } else {
            self.placeholder = str
        }
    }
}


public extension String {
    static func JSONSerializ(withObject object:Any) throws -> String? {
        let x =  try JSONSerialization.data(withJSONObject: object, options: .prettyPrinted)
        return String.init(data: x, encoding: .utf8)
    }
    
    func parseJSONString() throws -> Any? {
        guard let data = data(using: .utf8) else{
            return nil
        }
        
        return try JSONSerialization.jsonObject(with: data, options: .mutableLeaves)
    }
    
    var predicate: NSPredicate {
        return NSPredicate(format: "SELF MATCHES %@", argumentArray: [self])
    }
    
    func getImage() -> UIImage? {
        return UIImage(named: self)
    }
    
    func image() -> UIImage? {
        return self.getImage()
    }
    
    
    func attrString(attr: [NSAttributedString.Key: Any]? = nil) -> NSMutableAttributedString {
        return NSMutableAttributedString(string: self, attributes: attr)
    }
}

public func + (left: NSMutableAttributedString, right: NSMutableAttributedString) -> NSMutableAttributedString {
    left.append(right)
    return left
}


//for cell identify

public protocol ReuseableIdentifier: AnyObject {
    static var identifier: String {get}
}

public extension ReuseableIdentifier {
    static var identifier: String {
        let name = NSStringFromClass(Self.self)
        let names = name.split(separator: ".")
        return String(names.last ?? "")
    }
}


extension UITableViewCell: ReuseableIdentifier {}
extension UICollectionViewCell: ReuseableIdentifier {}


public func getUnion<T: Hashable>(lhs: [T], rhs: [T]) -> (Set<T>, Set<T>, Set<T>, Set<T>) {
    let aSet = Set(lhs)
    let bSet = Set(rhs)
    return (aSet.union(bSet), bSet.subtracting(aSet), aSet.subtracting(bSet), aSet.intersection(bSet))
}

public extension Array {
    func nextIndex(index: Int) -> Int {
        var newIndex = index + 1
        if newIndex >= self.count {
            newIndex = 0
        }
        
        return newIndex
    }
    
    func preIndex(index: Int) -> Int {
        var newIndex = index - 1
        if newIndex < 0 {
            newIndex = self.count - 1
        }
        
        return newIndex
    }
}


public extension String {
    // 直接给String扩展方法
    func md5() -> String {
        let str = self.cString(using: String.Encoding.utf8)
        let strLen = CUnsignedInt(self.lengthOfBytes(using: String.Encoding.utf8))
        let digestLen = Int(CC_MD5_DIGEST_LENGTH)
        let result = UnsafeMutablePointer<UInt8>.allocate(capacity: 16)
        CC_MD5(str!, strLen, result)
        let hash = NSMutableString()
        for i in 0 ..< digestLen {
            hash.appendFormat("%02x", result[i])
        }
        free(result)
        return String(format: hash as String)
    }
}

public extension Int {
    func bit_Money_price(reserve: Int) -> String {
        let d = Double(self) / 100.0
        let formatStr = "%.\(reserve)f"
        return String.init(format: formatStr, d)
    }
}

public extension Int {
    
    func countString() -> String {
        if self < 10000 {
            return self.description
        }
        
        let wanCount = self / 10000
        return wanCount.description + "万"
    }
}




public extension Array {
    
    static func <<(l:Array<Element>, r:Int) -> Array<Element> {
        var tmp = [Element]()
        for i in 0..<l.count {
            var index = i + 1
            if index == l.count {
                index = 0
            }
            tmp.append(l[index])
        }
        return tmp
    }
    
    static func >>(l:Array<Element>, r:Int) -> Array<Element> {
        var tmp = [Element]()
        for i in 0..<l.count {
            var index = i - 1
            if index == -1 {
                index = l.count - 1
            }
            tmp.append(l[index])
        }
        return tmp
    }
}

